package com.wzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzy.entity.Hero;
import com.wzy.mapper.HeroMapper;
import com.wzy.service.HeroService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.util.Const;
import com.wzy.util.Pager;
import com.wzy.util.textTranfromHtmlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2021-02-09
 */
@Service

public class HeroServiceImpl extends ServiceImpl<HeroMapper, Hero> implements HeroService {

    @Resource
    private HeroMapper heroMapper;

    @Autowired
    private  RedisTemplate<String, Object> redisTemplate;

    @Override
    public void getHeroListByProviceId(Long provinceId, Page<Hero> heroPage) {
        QueryWrapper<Hero> wrapper = new QueryWrapper<>();
//        等于对应的省份id
        wrapper.eq("province_id",provinceId);
//        分页查询

//        hero_id,
//                user_id,
//                province_id,
//                battle_id,
//                title,
//                description,
//                img,
//                comment_count,
//                create_time
        wrapper.select("hero_id","user_id","province_id","title",
                "description","img","comment_count","create_time");
        baseMapper.selectPage(heroPage,wrapper);
    }

    @Override
    public Pager<Hero> list(Hero conition, Integer page) {
        List<Hero> datas=this.heroMapper.selectByCondition(conition, (page-1)* Const.PAGE_SIZE, Const.PAGE_SIZE);
        int size=this.heroMapper.sizeByCondition(conition);
        return new Pager<>(datas, size, page, Const.PAGE_SIZE);
    }

    @Override
    public int append(Hero hero) {
        Set<String> keys = redisTemplate.keys("*hero*");
        redisTemplate.delete(keys);
        hero.setViews(0);
        hero.setCommentCount((long)0);
        hero.setCreateTime((new Date()));
        return this.baseMapper.insert(hero);
    }

    @Override
    public Hero detail(Long heroId) {
        return this.heroMapper.selectById(heroId);
    }

    /**
     * 根据heroId查询hero并加入缓存
     * @param heroId
     * @return
     */
    @Override
    public Hero getHeroById(Long heroId) {
        //先去查缓存
        Hero hero=null;
        hero= (Hero) redisTemplate.opsForValue().get("hero"+String.valueOf(heroId));
        //没查到去数据库中去查
        if(hero==null){
          hero= baseMapper.selectById(heroId);
            String content=textTranfromHtmlUtils.markdownToHtmlExtensions(hero.getContent());
          hero.setContent(content);
            redisTemplate.opsForValue().set("hero"+String.valueOf(heroId),hero);
            //设置缓存为5分钟
            redisTemplate.expire(String.valueOf(heroId),5, TimeUnit.MINUTES);
        }
        return hero;
    }
}
